package com.yjps.system.controller;

import com.alibaba.fastjson.JSON;
import com.yjps.common.message.Message;
import com.yjps.common.page.Pager;
import com.yjps.common.util.CommonUtil;
import com.yjps.system.model.Recruitment;
import com.yjps.system.service.RecruitmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsFileUploadSupport;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;

/**
 * 招聘控制类
 */
@Controller
public class RecruitmentController {
    private static final Logger log = LoggerFactory.getLogger(RecruitmentController.class);

    @Autowired
    RecruitmentService recruitmentService;

    /**
     * 后台管理招聘列表
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/system/recruitment/list", method = RequestMethod.GET)
    public String getList(Model model,HttpServletRequest request,HttpServletResponse response) {
        Pager<Recruitment> pager = recruitmentService.getRecruitmentByPager(new Pager<Recruitment>(request,response));
        model.addAttribute(pager);
        return "system/recruitment/list";
    }

    /**
     * 请求编辑招聘信息页面
     * @return
     */
    @RequestMapping(value = "/system/recruitment/edit",method = RequestMethod.GET)
    public String edit(){
        return "system/recruitment/edit";
    }

    /**
     * 添加招聘信息
     * @param recruitment
     * @return
     */
    @RequestMapping(value = "/system/recruitment/edit",method = RequestMethod.POST)
    public String edit(Recruitment recruitment){
        recruitment.setIsValid(1);
        recruitment.setState(true);
        recruitment.setAnnounceTime(CommonUtil.dataToString(new Date()));
//        recruitment.setDeadline(CommonUtil.dataToString(new Date()));
        recruitmentService.add(recruitment);
        log.debug("招聘信息添加成功");
        return "redirect:/system/recruitment/list";
    }

    /**
     * 更新招聘信息
     * @param recruitment
     * @return
     */
    @RequestMapping(value = "/system/recruitment/update",method = RequestMethod.POST)
    public String update(Recruitment recruitment){
        recruitmentService.update(recruitment);
        log.debug("更新招聘信息 id为  " + recruitment.getId()  + " 更新成功");
        return "redirect:/system/recruitment/list";
    }

    /**
     * 获取指定招聘对象id的json数据
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/system/recruitment/getRecruitment",method = RequestMethod.GET)
    public String getRecruitment(@RequestParam(value = "id",required = true)String id,HttpServletResponse response){
        Recruitment recruitment = recruitmentService.find(Integer.parseInt(id));
        log.debug("成功获取指定招聘对象id的json数据");
        return recruitment.toJson();
    }

    /**
     *
     * 删除指定招聘id的招聘信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/system/recruitment/delete",method = RequestMethod.GET)
    public String delete(@RequestParam(value = "id",required = true)String id){
        recruitmentService.delete(recruitmentService.find(Integer.parseInt(id)));
        log.debug("删除成功");
        return "redirect:/system/recruitment/list";
    }

    /**
     * 公司形象前台 分页获取招聘信息列表
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/company/recruitment/list.do",method = RequestMethod.GET)
    public String list(@RequestParam(value = "pageNo",required = false)String pageNo,HttpServletRequest request,HttpServletResponse response){
        System.out.println("pageno = " + request.getParameter("pageNo"));
        Message message = null;
        try {
            Pager pager = recruitmentService.getRecruitmentByPager(new Pager<Recruitment>(request, response));
            System.out.println("jsjoon = " + JSON.toJSONString(pager));
            message = new Message(true,"get recruitment success",pager);
        }catch (Exception e){
            message = new Message(false,"get recruitment fail",null);
        }
        return message.toJson();
    }

    /**
     * 获得指定id的招聘对象json数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/company/recruitment/get.do",method = RequestMethod.GET)
    @ResponseBody
    public String get(@RequestParam(value = "id",required = true)String id,HttpServletResponse response){
        Message message = null;
        try {
            Recruitment recruitment = recruitmentService.find(Integer.parseInt(id));
            message = new Message(true,"成功获得指定id的招聘对象json数据",recruitment);
        }catch (Exception e){
            message = new Message(false,"获取失败");

        }
        return message.toJson();
    }


}
